package adc.budget.ui.web.wicket;

import java.util.Set;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import adc.app.ui.wicket.BoEditPanel.ICancelCallback;
import adc.app.ui.wicket.BoEditPanel.ISaveCallback;
import adc.app.ui.wicket.BoListPanel.IAddCallback;
import adc.app.ui.wicket.BoListPanel.IDeleteCallback;
import adc.app.ui.wicket.BoListPanel.IEditCallback;
import adc.app.ui.wicket.model.BoModel;
import adc.budget.spec.bo.IAccountType;
import adc.budget.spec.bs.IAccountTypeBs;
import adc.budget.ui.web.wicket.account.AccountTypeEditPanel;
import adc.budget.ui.web.wicket.account.AccountTypeListPanel;
import adc.budget.ui.web.wicket.account.IMsgsAccount;

public class AccountTypePage extends LayoutPage implements IAddCallback, IEditCallback<IAccountType>, IDeleteCallback, ISaveCallback<IAccountType>, ICancelCallback {

    private static final long serialVersionUID = 1L;

    @SpringBean
    private IAccountTypeBs accountTypeBs;
    private final AccountTypeListPanel listPanel;
    
    public AccountTypePage() {
        super();
        listPanel = new AccountTypeListPanel("accountTypes");
        listPanel.setAddCallback(this).setEditCallback(this).setDeleteCallback(this);
        listPanel.setOutputMarkupId(true);
        add(listPanel);
        setTitle(new ResourceModel(IMsgsAccount.ACCOUNT_TYPES));
    }

    @Override
    public void add(AjaxRequestTarget target) {
        final IModel<IAccountType> accountType = new BoModel<IAccountType>(IAccountTypeBs.class);
        openEditDialog(target, accountType);
    }

    @Override
    public void edit(AjaxRequestTarget target, IModel<IAccountType> accountType) {
        openEditDialog(target, accountType);
    }

    @Override
    public void delete(AjaxRequestTarget target, Set<Long> ids) {
        for (Long id : ids) {
            final IAccountType at = accountTypeBs.byId(id);
            if (at != null) {
                accountTypeBs.delete(at);
            }
        }
        listPanel.updateTable(target);
    }

    @Override
    public void save(AjaxRequestTarget target, Form<IAccountType> form) {
        accountTypeBs.save(form.getModelObject());
        getModalWindow().close(target);
        listPanel.updateTable(target);
    }

    @Override
    public void cancel(AjaxRequestTarget target) {
        getModalWindow().close(target);
    }

    private void openEditDialog(final AjaxRequestTarget target, final IModel<IAccountType> accountType) {
        final ModalWindow md = getModalWindow();
        md.setContent(new AccountTypeEditPanel(md.getContentId()).setSaveCallback(this)
                .setCancelCallback(this).setModel(accountType));
        md.setInitialWidth(200);
        md.setInitialHeight(120);
        md.setTitle(new ResourceModel(IMsgsAccount.ACCOUNT_TYPE));
        md.show(target);
    }

}
